﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Runtime.InteropServices;

namespace dreametryVersion2
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        private void frmLogin_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if(txtUsername.Text == "" || txtPassword.Text == "")
            {
                lblMessage.Text = "Please fill all fields to continue";
            }
            else
            {
                string commandText = "SELECT * FROM employee WHERE username = '"+txtUsername.Text+"' and password='"+(global.CalculateMD5Hash(txtPassword.Text))+"'";
           
                using (SqlConnection connection = new SqlConnection(global.conString))
                {
                    SqlCommand command = new SqlCommand(commandText, connection);
                    command.Connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    
                    while (reader.Read())
                    {
                        global.employeeID = reader["emp_id"].ToString();
                        global.fullname = reader["fullname"].ToString();
                        global.folder = reader["folder"].ToString();
                        global.picture = (byte[])reader["picture"];
                        global.themeColor = reader["color_scheme"].ToString();
                    }
                    
                    if(global.fullname != null)
                    {
                        frmMain form = new frmMain();
                        form.Show();
                        this.Hide();
                    }
                    else
                    {
                        txtPassword.Text = "";
                        lblMessage.Text = "wrong username or password";
                    }
                }
            }
        }

        private void txtUsername_TextChanged(object sender, EventArgs e)
        {
            if(lblMessage.Text != null)
            {
                lblMessage.Text = null;
            }
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            if (lblMessage.Text != null)
            {
                lblMessage.Text = null;
            }
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                btnLogin.PerformClick();
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin.PerformClick();
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                Random rand = new Random();
                string[] files = Directory.GetFiles(@"\\DB-SERVER\Users\Public\Documents\MIS-LP", "*", SearchOption.AllDirectories);
                pbLogin.Image = Image.FromFile(files[rand.Next(files.Length)]);
            }
            catch
            {
                pbLogin.ImageLocation = @"\\DB-SERVER\Users\Public\Documents\MIS-LP\dreametry.png";
            }
        }
    }
}
